//
//  EditNameViewController.m
//  RENTVISITOR
//
//  Created by wonder on 2017/8/28.
//  Copyright © 2017年 UNN. All rights reserved.
//

#import "EditNameViewController.h"
#import "RentVieitor.h"

@interface EditNameViewController ()<UITextFieldDelegate>

@end

@implementation EditNameViewController {
    
    UITextField *nameText;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"修改昵称";
    self.view.backgroundColor = BackgroundColor;
    [self creatLeftView];
    [self creatView];
}

- (void)creatLeftView {
    
    // 自定义导航栏左侧按钮
    UIButton * leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    leftBtn.frame = CGRectMake(0, 0, 10,15 );
    [leftBtn setBackgroundImage:[UIImage imageNamed:@"箭头左"] forState:UIControlStateNormal];
    [leftBtn addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem * leftItem = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];
    self.navigationItem.leftBarButtonItem = leftItem;
    
    // 自定义导航栏左侧按钮
    UIButton * rightBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    rightBtn.frame = CGRectMake(0, 0, 50, 20);
    [rightBtn setTitle:@"保存" forState:UIControlStateNormal];
    [rightBtn setTitleColor:[UIColor colorWithRed:99/255.0 green:99/255.0 blue:99/255.0 alpha:1] forState:UIControlStateNormal];
    [rightBtn addTarget:self action:@selector(saveAction) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem * rightItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
    self.navigationItem.rightBarButtonItem = rightItem;
    
}

- (void)creatView {
    
    UIView *bdView = [[UIView alloc] initWithFrame:CGRectMake(0, 10, KWidth, 50)];
    bdView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:bdView];
    
    nameText = [[UITextField alloc] initWithFrame:CGRectMake(20, 10, KWidth-30, 30)];
    nameText.text = self.name;
    nameText.delegate = self;
    nameText.textColor = [Utils colorConvertFromString:@"#333333"];
    nameText.font = [UIFont systemFontOfSize:15];
    
    UIButton *canceBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    canceBtn.frame = CGRectMake(0, 0, 15, 15);
    [canceBtn setBackgroundImage:[UIImage imageNamed:@"关闭"] forState:UIControlStateNormal];
    [canceBtn addTarget:self action:@selector(canceAction) forControlEvents:UIControlEventTouchUpInside];
    nameText.rightView = canceBtn;
    nameText.rightViewMode = UITextFieldViewModeAlways;
    [bdView addSubview:nameText];
    
}


//保存
- (void)saveAction {
    
    
    if (nameText.text.length == 0) {
        
        [APPDELEGATE showPro:@"请输入昵称"];
        return;
    }
    
    NSDictionary *dataDic = User_listsValue;
    NSString *url = [NSString stringWithFormat:@"%@member/info/nickname",Main_Url];
    NSString *number = dataDic[@"number"];
    NSString *token = dataDic[@"token"];
    NSString *nickname = [nameText.text stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    NSString *md5String = [NSString stringWithFormat:@"%@%@%@%@%@",nickname,number,token,APPKEY_URL,url];
   
    //32位小写

    
    md5String = [LCMD5Tool MD5ForLower32Bate:md5String];
    NSDictionary *param = @{
                            @"number":number,
                            @"token":token,
                            @"nickname":nickname,
                            @"sign":md5String
                            };
    [AFHttpTool requestWihtUrl:url params:param success:^(id response) {
        

        if ([response[@"code"] isEqualToString:@"10000"]) {
            
            [APPDELEGATE showEditSuccess:@"修改成功"];
            [[NSNotificationCenter defaultCenter] postNotificationName:@"update" object:nil userInfo:nil];
            NSDictionary *dic = @{
                                  @"name":nameText.text
                                  };
            [[NSNotificationCenter defaultCenter] postNotificationName:@"upName" object:nil userInfo:dic];
        }else {
            
            [APPDELEGATE showPro:response[@"message"]];
        }
        
        
    } failure:^(NSError *err) {
        
        
    }];

    
}

//取消
- (void)canceAction {
    
    nameText.text = @"";
}

- (void)backAction {
    
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
    [self.view endEditing:YES];
}

@end
